package com.ws.shop.action.admin;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.Accessory;
import com.ws.shop.entity.Article;
import com.ws.shop.entity.Goods;
import com.ws.shop.entity.Mall;
import com.ws.shop.service.GoodsService;
import com.ws.shop.service.MallService;
import com.ws.shop.util.RequestUtil;
import com.ws.shop.util.SpringUtil;
import com.ws.shop.util.StringUtil;

@Controller
@RequestMapping(value="/admin/mall/*")//
public class MallAction extends BaseAdminAction{
	private static Logger log = Logger.getLogger(MallAction.class);
	@Autowired
	private MallService mallService;
	@Autowired
	private GoodsService goodsService;
	
	
	@RequestMapping(value="index.jhtml",method=RequestMethod.GET)
	public String index(HttpServletRequest request,Model model){
		String title = RequestUtil.getStringParameter(request, "goodsname", null);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		Page page = mallService.findMallPageByTitle(pageno,0,title);
		model.addAttribute(Page.PAGE, page);
		model.addAttribute("goodsname", title);
		model.addAttribute("stateDescs", Mall.STATE_DESCS);
		return "mall_index";
	}

	@RequestMapping(value="input.jhtml",method=RequestMethod.GET)
	public String input(HttpServletRequest request,Model model){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		int gid = RequestUtil.getIntParameter(request, "gid", 0);
		if(id == 0){
			Goods goods = goodsService.findGoodsById(gid);
			if(goods == null){
				model.addAttribute(ERROR_MSG, "您选择的商品不存在");
				return ERROR;
			}
			model.addAttribute("goods", goods);
		}else if(id > 0){
			Mall mall = mallService.findMallById(id);
			if(mall == null){
				model.addAttribute(ERROR_MSG, "您选择的商品不存在");
				return ERROR;
			}
			gid = mall.getGid();
			model.addAttribute("mall", mall);
		}else{
			model.addAttribute(ERROR_MSG, "参数错误");
			return ERROR;
		}
		List<Accessory> accessoryList = goodsService.findAccessoryListByGoodsId(gid);
		model.addAttribute("accessoryList", accessoryList);
		model.addAttribute("id", id);
		model.addAttribute("saveImagUrl",AccessoryAction.SAVE_IMAG_URL);
		return "mall_input";
	}
	
	@RequestMapping(value="save.jhtml",method=RequestMethod.POST)
	public String save(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		int gid = RequestUtil.getIntParameter(request, "gid", 0);
		String title = RequestUtil.getStringParameter(request, "title", null);
		int goodspic = RequestUtil.getIntParameter(request, "goodspic", 0);
		float costprice = RequestUtil.getFloatParameter(request, "costprice", 0f);
		float price = RequestUtil.getFloatParameter(request, "price", 0f);
		int inventory = RequestUtil.getIntParameter(request, "inventory", 0);
		int recommend = RequestUtil.getIntParameter(request, "recommend", 0);
		int state = RequestUtil.getIntParameter(request, "state", 1);
		int type = RequestUtil.getIntParameter(request, "type", 0);
		if(gid > 0){
			try{
				Goods goods = goodsService.findGoodsById(gid);
				if(goods == null)
					return this.ajaxJsonErrorMessage(response, "商品不存在");
				int currentnum = 0;
				if(title == null)
					title = goods.getGoodsname();
				Mall mall = new Mall(id,gid,title,goodspic,costprice,price,inventory,currentnum,recommend,0,type,state,"");
				try{
					if(id > 0){
						Mall oldmall = mallService.findMallById(id);
						if(oldmall == null){
							return this.ajaxJsonErrorMessage(response, "您要编辑的积分商品不存在！");
						}
						if(!StringUtil.isEmpty(oldmall.getHtml())){
							File file = new File(SpringUtil.getRealPath(oldmall.getHtml()));
							if(file.exists() && file.isFile())
								file.delete();
							mall.setHtml(oldmall.getHtml());
						}
					}
					mallService.saveOrUpdate(mall);
					return this.ajaxJsonSuccessMessage(response, "操作成功");
				}catch(Exception e){
					log.error("更新商城失败："+mall,e);
					return this.ajaxJsonErrorMessage(response, "系统异常，请稍后再试");
				}
			}catch(Exception e){
				log.error("查询商品信息失败gid="+gid,e);
				return this.ajaxJsonErrorMessage(response, "系统异常，请稍后再试");
			}
		}else{
			return this.ajaxJsonErrorMessage(response, "商品不存在");
		}
	}
	
}
